import Vue from 'vue';
import routes from "router";
import Vonic from 'vonic';
import axios from 'axios';
import "config/rem.js";

// axios.defaults.baseURL = 'https://api.huoxingy.com/v1/member/';

// 


// 根据环境来选择api地址
let baseUrl = window.location.hostname;
console.log(baseUrl)
switch (baseUrl) {
    case "localhost":
        axios.defaults.baseURL = 'https://staging-api.huoxingy.com/v1/member/';
        break;
    case "staging-membership.huoxingy.com":
        axios.defaults.baseURL = 'https://staging-api.huoxingy.com/v1/member/';
        break;
    case "prod-test-membership.huoxingy.com":
        axios.defaults.baseURL = 'https://prod-test-api.huoxingy.com/v1/member/';
        break;
    default:
        axios.defaults.baseURL = 'https://prod-api.huoxingy.com/v1/member/';
        break;
}
//


// 



axios.defaults.headers.post['Content-Type'] = 'application/json';

/*
    设置请求拦截器
*/
axios.interceptors.request.use(function(config) {
    let token = localStorage.getItem('token');
    if (token) {
        config.headers.Authorization = token;
    }
    return config;
}, function(error) {
    return Promise.reject(error);
});

/*
    添加响应拦截器
*/
axios.interceptors.response.use(function(response) {
    return response;
}, function(error) {
    if (error.response) {
        let tipText;
        error.response.data.message?tipText=error.response.data.message:tipText=error.response.data;
        if ( error.response.status == 401 ) {
            window.location.href = window.location.href.split('#')[0]+'#/login';
            tipText = '帐号失效，请重新登录';
        } else if ( error.response.status == 403 ) {
            tipText = '权限不足';
        }
        $toast.show(tipText, 2000).then(() => {});
    }
    return Promise.reject(error);
});
Vue.prototype.$http = axios;

Vue.filter('numberToAllDate', function ( val ) {
    if ( !val ) {
        return '';
    }
    let date = new Date(val);
    let dateStr =date.getFullYear()+'.'+(date.getMonth()+1)+'.'+date.getDate()+'-'+ (date.getHours()<10?'0'+date.getHours():date.getHours()) + ':' + (date.getMinutes()<10?'0'+date.getMinutes():date.getMinutes());
    return dateStr;
})

Vue.prototype.$uuid = function ( len, radix ) {
    var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
    var uuid = [], i;
    radix = radix || chars.length;
    if (len) {
        // Compact form
        for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random()*radix];
    } else {
        var r;
        uuid[14] = '4';
        for ( i = 0; i < 32; i++ ) {
            if ( !uuid[i] ) {
                r = 0 | Math.random()*62;
                uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
            }
        }
    }
    return uuid.join('');
}

Date.prototype.Format = function (fmt) { //author: meizz 
    var o = {
        "M+": this.getMonth() + 1, //月份 
        "d+": this.getDate(), //日 
        "h+": this.getHours(), //小时 
        "m+": this.getMinutes(), //分 
        "s+": this.getSeconds(), //秒 
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
        "S": this.getMilliseconds() //毫秒 
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}


// Services
// import $sidebar from './services/sidebar/index.js'
// Services
// Vue.prototype.$sidebar = window.$sidebar = $sidebar

// Components
import Datepicker from './components/datepicker'
// Components
Vue.component('datepickerC', Datepicker)


const is_ios = () => {
  return /iPad|iPhone|iPod/.test(navigator.userAgent)
}


// 一些兼容性代码
/* for iOS 10, users can now pinch-to-zoom even when a website sets user-scalable=no in the viewport. */
document.documentElement.addEventListener('touchstart', (e) => {
  if (e.touches.length > 1) {
    e.preventDefault()
  }
}, false)

/* iOS Safari - Disable double click to zoom */
if (is_ios()) {
  let lastTouchEnd = 0;
  document.documentElement.addEventListener('touchend', (e) => {
    let now = (new Date()).getTime()
    if (now - lastTouchEnd < 300) {
      e.preventDefault()
    }
    lastTouchEnd = now
  }, false)
}


Vue.use(Vonic.app, {
  routes
})
